﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace auxiliary_proj
{
    public static class Singleton
    {
        private static Home home;
        private static FormOrderSummary orderSummary;
        private static FormViewPO formViewPO;
        private static MainForm mf;
        private static UCInventory ucInv;
        private static UCOrderStatus ucOS;
        private static UCPurchaseOrder ucPO;
        private static UCReport ucRep;
        private static UCUnits ucUnits;
        private static UCViewVendor ucVendors;
        private static FormShowMessage showMessage;

        public static void disposeObjects()
        {
            home = null;
            orderSummary = null;
            formViewPO = null;
            ucInv = null;
            ucOS = null;
            ucPO = null;
            ucRep = null;
            ucUnits = null;
            ucVendors = null;
            showMessage = null;
        }

        /// <summary>
        /// use generics to create a new form
        /// only allow 1 popup to exist
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void showForm<T>(FormCollection open) where T : new()
        {
            //creates single instance of type <T> (first of that type) from the collection of open forms
            T genericForm = open.OfType<T>().FirstOrDefault();

            //removed a lot of code and replaced with below
            //initially was testing typeof(<T>) == typeOf(formName)
            //so there was 1 if/else for each form I wanted to limit
            //made possible by 'where <T> : new()' allowing for instantiation of <T>
                if (genericForm == null)
                {
                    T a  = new T();
                    Form b = a as Form;
                    b.Show();
                }
                else
                {
                    Form a = genericForm as Form;
                    a.Focus();
                }  
        }

        public static void showInvItems(int item_id, string name, double price)
        {
            FormViewInventory exists = Application.OpenForms.OfType<FormViewInventory>().FirstOrDefault();
            if (exists == null)
            {
                FormViewInventory fvi = new FormViewInventory(item_id, name, price);
                fvi.StartPosition = FormStartPosition.CenterScreen;
                fvi.Show();
            }
            else
            {
                exists.Close();
                exists.Dispose();
                FormViewInventory fvi = new FormViewInventory(item_id, name, price);
                fvi.StartPosition = FormStartPosition.CenterScreen;
                fvi.Show();
            }
        }

        public static Home getHome()
        {
            if (home == null)
                home = new Home();
            return home;
        }

        public static FormOrderSummary getFormOrderSummary()
        {
            if (orderSummary == null)
                orderSummary = new FormOrderSummary();
            return orderSummary;
        }

        public static FormViewPO getViewPO()
        {
            if (formViewPO == null)
                formViewPO = new FormViewPO();
            return formViewPO = new FormViewPO();
        }

        public static MainForm getMainForm()
        {
            if (mf == null)
                mf = new MainForm();
            return mf;
        }

        public static UCInventory getUCInventory()
        {
            if (ucInv == null)
                ucInv = new UCInventory();
            return ucInv;
        }

        public static UCOrderStatus getUCOrderStatus()
        {
            if (ucOS == null)
                ucOS = new UCOrderStatus();
            return ucOS;
        }

        public static UCPurchaseOrder getUCPurchaseOrder()
        {
            if (ucPO == null)
                ucPO = new UCPurchaseOrder();
            return ucPO;
        }

        public static UCReport getUCReport()
        {
            if (ucRep == null)
                ucRep = new UCReport();
            return ucRep;
        }

        public static UCUnits getUCUnits()
        {
            if (ucUnits == null)
                ucUnits = new UCUnits();
            return ucUnits;
        }

        public static UCViewVendor getUCVendors()
        {
            if (ucVendors == null)
                ucVendors = new UCViewVendor();
            return ucVendors;
        }

        public static FormShowMessage getFormShowMessage()
        {
            if (showMessage == null)
                showMessage = new FormShowMessage();
            return showMessage;
        }
    }
}
